package com.jorado.redis.statistics;


import com.jorado.core.statistics.interfaces.RedisStatisticsService;
import com.jorado.core.statistics.model.RedisStatistics;
import com.jorado.metrics.type.Summary;
import com.jorado.metrics.util.TimeHelp;

/**
 *
 */
public class SimpleRedisStatisticsService implements RedisStatisticsService {

    private static RedisStatisticsService redisStatistics = new SimpleRedisStatisticsService();

    public SimpleRedisStatisticsService() {
        Summary.getInstance().register("common_redis_request", "redisAPM", true, "server_type", "target_ip", "command", "status");
    }

    @Override
    public void statistics(final RedisStatistics redisStatistics) {
        double second = TimeHelp.milliToSecond(redisStatistics.getCostTime());
        String status = redisStatistics.getStatus();
        String serverIp = redisStatistics.getServerIp();
        String[] serverIps = redisStatistics.getServerIp().split(",");
        if (serverIps.length > 0) {
            serverIp = serverIps[0];
        }
        String command = redisStatistics.getCommand();
        Summary.getInstance().observe("common_redis_request", second, "redis", serverIp, command, status);
    }

    public static RedisStatisticsService instance() {
        return redisStatistics;
    }

}
